
import * as PIXI from 'pixi.js';
import FrameList from '../../frameList';
import {FRAMES1, FRAMES2} from 'common/const';
// import {FRAMES11, FRAMES22} from 'common/const';

export default class OneContainer {
  constructor(props) {

    this.Container = PIXI.Container;
    this.loader = PIXI.loader;
    this.resources = PIXI.loader.resources;
    this.Sprite = PIXI.Sprite;


    let xu = props.item.xulie;
    this.packets = new this.Container();

    let pac = new this.Sprite(this.resources[props.item.url].texture);

    pac.width = props.item.width;
    pac.height = props.item.height;
    pac.rotation = props.item.xulie === 0 ? this.randomPos('', 1, 10) : 5.5; // 金币旋转角度随机
    pac.x = this.randomPos('x', -60, 5);
    pac.y = this.randomPos('y', -30, 5);

    // console.log('xy', pac.x, pac.y);

    pac.interactive = true;
    pac.on('pointerdown', () => {
      // pac.visible = false;
      // props.addIndex(); // 记录点击次数
      if (props.item.isClick) { // 是否可点击
        this.pixiFrame(xu); // 添加序列帧
        // this.showImg(); // 点击出分数图
        this.packets.removeChild(pac);
      }

    });

    this.packets.addChild(pac);


    props.App.ticker.add(() => { // 控制红包速度
      if (xu === 0) { // 金币
        this.packets.x += 135 * 2 / props.item.width;
        this.packets.y += 146 * 2 / props.item.height;
      } else {
        this.packets.x += 500 * 2.5 / props.item.width; // 得到的是2,1.6,1.4这种数
        this.packets.y += 714 * 2.5 / props.item.height;
      }

    });

  }

  pixiFrame = (xulie) => {

    let list = xulie === 1 ? FRAMES1 : FRAMES2;
    // let list = xulie === 1 ? FRAMES11 : FRAMES22;
    let frameList = new FrameList({
      list: list.map(item => item.url),
      loop: false,
      width: this.packets.children[0].width,
      height: this.packets.children[0].height,
      left: this.packets.children[0].x,
      top: this.packets.children[0].y,
      rotate: 5.5,
      speed: 0.2,
      // onStop: this.props.onTransitionEnd,
      // onComplete: () => this.completePlay(ind),
      onComplete: () => this.packets.removeChild(frameList),

    });

    frameList.interactive = true;
    frameList.on('pointerdown', () => {
      // console.log('点我没效果哦');
    });

    this.packets.addChild(frameList);

  };

  // showImg = () => {
  //   // let imgs = ['/src/assert/img/otherImgs/qie2.png', '/src/assert/img/otherImgs/qie4.png'];
  //   let imgs = ['assets/qie2.png', 'assets/qie4.png']; // 线上
  //   let randomImg = Math.floor(this.randomPos('', 1, 3) - 1);
  //   let img = new this.Sprite(this.resources[imgs[randomImg]].texture);
  //   img.width = 418;
  //   img.height = 372;
  //   img.x = this.packets.children[0].x + 100;
  //   img.y = this.packets.children[0].y - 100;
  //   this.packets.addChild(img);
  //
  //   setTimeout( () => this.packets.removeChild(img), 1000 );
  //
  // };

  randomPos = (xy, min, max) => {
    if (xy === 'x') return (Math.random() * (max - min) + min) * 108;
    if (xy === 'y') return (Math.random() * (max - min) + min) * 192;
    if (xy === '') return Math.random() * (max - min) + min;
  };



}
